/**
 * Genarator by KKDGen v<?php echo $version."\n";?>
 * @author KKDeveloeper
 * @email Dixonsatit@gmail.com
 * @link www.ikkdev.com,www.kkdeveloper.com
 <?php  echo empty($nameSpace)?'':'* Namespace : '.$nameSpace."\n"; ?>
 * ExtjsModel : <?php echo $modelClass."\n";?>
 */

Ext.define("<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.model.'.$modelClass; ?>", {
extend: "Ext.data.Model",
fields: [
<?php 
$count = count($columns);
$i=1;
foreach ($columns as $column): 
    if($column->isPrimaryKey===true)
    {
        $pk= $column->name;
    }
    if($column->type=='integer')$type='int';
    elseif($column->type=='date')$type='date';
    else $type='string';
    
    $dot = $count==$i?"\n":",\n";
    if(!is_null($column->defaultValue) or !empty ($column->defaultValue))
        echo " { name : '$column->name', type : '$type', defaultValue : '$column->defaultValue' }".$dot;   
    else 
        echo " { name : '$column->name', type : '$type' }".$dot;
    $i++;
    
endforeach; 
?>
],
    idProperty : '<?php echo $pk; ?>',
    proxy: {
        type: 'ajax',
        idProperty: '<?php echo $pk; ?>',
        //  url : 'index.php?r=<?php echo strtolower($modelClass);  ?>',
       /* actionMethods: {
            create : 'POST',
            read   : 'POST',
            update : 'POST',
            destroy: 'POST'
        },*/
        api: {
            read: 'index.php?r=<?php echo strtolower($modelClass);  ?>',
            create: 'index.php?r=<?php echo strtolower($modelClass);  ?>/create',
            update: 'index.php?r=<?php echo strtolower($modelClass);  ?>/update',
            destroy: 'index.php?r=<?php echo strtolower($modelClass);  ?>/delete'
        },
        reader: {
            type : 'json',
            root : 'data',
            totalProperty  : 'total',
            successProperty: 'success',
            messageProperty: 'message'
        },
        writer: {
            type: 'json',
            writeAllFields: false,
            root: 'data'
        }, 
         extraParams: {
           // q: 'test'
        },
        listeners: {
            exception: function(proxy, response, operation){
                Ext.MessageBox.show({
                    title: 'REMOTE EXCEPTION',
                    msg: operation.getError(),
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            }
        },
        afterRequest: function (request, success) 
        {
            
            if (request.action == 'read') {
                console.log('Read');
            }
            
            else if (request.action == 'create') {
                console.log('Create');
                if(<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.controller.'.$modelClass; ?>.rowEdit===true)
                {
                    var grid = Ext.ComponentQuery.query('<?php echo strtolower($modelClass);  ?>Grid')[0];
                    grid.plugins[0].startEdit(0, 1);
                }
                
            }
            
            else if (request.action == 'update') {
                console.log('Update');
            }
            
            else if (request.action == 'destroy') {
                console.log('Delete');
            }
        }
    }
});
